package com.dj.dormitory.controller;


import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.Org;
import com.dj.dormitory.entity.Selection;

import com.dj.dormitory.entity.req.SelectionReq;

import com.dj.dormitory.service.OrgService;
import com.dj.dormitory.service.SelectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/selection")
public class SelectionController {

    @Autowired
    private SelectionService selectionService;

    @Autowired
    private OrgService orgService;

    @RequestMapping("add")
    public Result add(@RequestBody Selection selection){
        //判断选择的是否是班级
        Org org = orgService.getById(selection.getClassId());
        if(org.getType()!=4){
            return Result.error().message("请选择班级，才可以哦");
        }
        selectionService.save(selection);
        return Result.ok().message("添加预选成功");
    }
    @RequestMapping("edit")
    public Result edit(@RequestBody Selection selection){
        //判断选择的是否是班级
        Org org = orgService.getById(selection.getClassId());
        if(org.getType()!=4){
            return Result.error().message("请选择班级，才可以哦");
        }
        selectionService.updateById(selection);
        return Result.ok().message("修改预选成功");
    }
    @DeleteMapping("deletePart/{selectionIdsStr}")
    public Result deletePart(@PathVariable(value = "selectionIdsStr") List<String> ids_str ){
        selectionService.removeByIds(ids_str);
        return Result.ok().message("批量删除预选成功");
    }
    @DeleteMapping("deleteOne/{id}")
    public Result deleteOne(@PathVariable(value = "id") String id){
        selectionService.removeById(id);
        return Result.ok().message("删除预选成功");
    }
    @PostMapping("getList")
    public Result getList(@RequestBody SelectionReq selectionReq){
        Map<String,Object> map = selectionService.getList(selectionReq);
        return Result.ok().data("selections",map.get("selections")).data("count",map.get("count"));
    }
}

